#import "ParsingUtilities.h"

/**
 * A ParsingUtilities is an object representing the generic
 * parsing functionality needed for the application.
 *
 * @author      Justin Hollander
 * @version     0.2.6
 * @since       0.2.6
 */
@implementation ParsingUtilities

/**
 * Attempts to match a given text with a range of valid subtext.
 * 
 * Logic is as follows:
 * character before is space : x
 * character before doesn't exist : y
 * character before is alphaNumericSpecial: z
 *
 * character after is space : w
 * character after doesn't exist : v
 * character after alphaNumericSpecial : q
 *
 * if [ [(x) || (y)] && [(w)|| (v)] ]
 *	 is a valid match and return YES
 * else
 *	 is not a valid match and return no
 *
 * @param subrange   the range of subtext to match
 * @param eventDesc  the text to use as a source
 * @return           whether the match is a success
 */
+(BOOL) isMatchByRange:(NSRange)subrange eventDesc:(NSString*)eventDesc{
	
	BOOL doesntExistLeft = (subrange.location == 0)? YES : NO;
	BOOL doesntExistRight = ( subrange.location+subrange.length == [eventDesc length])? YES : NO;
	BOOL spaceAfterExists = ((!doesntExistRight) && ([eventDesc characterAtIndex:subrange.location+subrange.length] == (unichar)' '))? YES : NO;
	BOOL spaceBeforeExists = ((!doesntExistLeft) && ([eventDesc characterAtIndex:subrange.location-1] == (unichar)' ' ))? YES : NO;
	
	
	if ((spaceBeforeExists || doesntExistLeft) && (spaceAfterExists || doesntExistRight)) {
		return YES;
	}
	else {
		return NO;
	}
}

/**
 * Parse a building name out of a given string of text.
 *
 * @param textString  the source string of text
 * @return            the building if found
 */
+(Building*)parseBuildingFromString:(NSString*)textString
{
	BuildingCollection* bc = [BuildingCollection sharedBuildingCollection];
	NSRange matchRange;
	NSArray* buildingList = [bc getDataTypes];
	if ( (textString != nil) && (bc != nil) ){
		NSArray* exclusionList = [NSArray arrayWithObjects:@"!",@"?",@",",@".",@"*",@"[",@"]",@"{",@"}",@"<",@">",@"(",@")",@";",@":", @"\"",@"'", nil];
		
		for(NSString* exclusionToken in exclusionList)
			textString = [textString stringByReplacingOccurrencesOfString:exclusionToken withString:@" "];
		
		
		for(Building* b in buildingList){
			matchRange = [textString rangeOfString:b.name options:NSCaseInsensitiveSearch];
			if (matchRange.location != NSNotFound){
				if ([ParsingUtilities isMatchByRange:matchRange eventDesc:textString]) {
					return b;
				}
			}
			else {
				for(NSString* s in b.buildingTags){
					matchRange = [textString rangeOfString:s options:NSCaseInsensitiveSearch];
					if (matchRange.location != NSNotFound) {
						if ([ParsingUtilities isMatchByRange:matchRange eventDesc:textString]) {
							return b;
						}
					}
				}
			}
		}
	}
	
	
	/*if ( (textString != nil) && (bc != nil) )
	{
		NSArray* buildingList = [bc getDataTypes];
		NSArray* textStringTokens = [[[textString stringByReplacingOccurrencesOfString:@"." withString:@""] 
									   lowercaseString] componentsSeparatedByString:@" "];
		
		for (NSObject* o in buildingList) {
			Building* b = (Building*)o;
			NSString* buildingName = [b.name lowercaseString];
			for(NSString* token in textStringTokens){
				if([buildingName isEqualToString:token])return b;
				for(NSString* n in b.buildingTags)
					if([[n lowercaseString] isEqualToString:[token lowercaseString]])return b;
			}
		}
		
	}*/
	
	return nil;
}

@end
